<?php

namespace teach\controllers;

use backend\models\MallMaterial;
use teach\service\Material;
use teach\util\helper\ResponseHelper;
use Yii;
use yii\filters\VerbFilter;
use yii\web\Controller;

class MaterialController extends Controller
{
    public $enableCsrfValidation = false;

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    '*' => ['post'],
                ],
            ],
        ];
    }

    /*
     * 获取菜单列表
     */
    public function actionMenu()
    {
        $request = Yii::$app->request;
        $rank = $request->post('rank');
        $parent_id = $request->post('parent_id');

        $msg = '';
        $flag = false;

        if (!$rank) {
            $msg = 'rank 不能为空';
        } elseif (empty(isset($parent_id))) {
            $msg = 'parent_id 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            $page = $request->post('page');
            $page = $page ? $page : 1;
            $map = " status = 1 AND  rank = {$rank} AND parent_id = {$parent_id} ";
            $materials = new Material();
            $material = $materials->menuList($map, $page, $pre_page = 20);
            if ($material) {
                $jsonData = ResponseHelper::buildJsonData(200, '成功', $material);
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '失败');
            }
        }

        return $this->asJson($jsonData);
    }

    /**
     * 获取素材列表
     * @return \yii\web\Response
     */
    public function actionList()
    {
        $request = Yii::$app->request;
        $mid = $request->post('mid');

        $msg = '';
        $flag = false;

        if (!$mid) {
            $msg = " mid 不能为空";
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(false, $msg);
        } else {
            $map = " m.mid = {$mid} AND m.status = 1 ";
            $page = $request->post('page');
            $page = $page ? $page : 1;
            $materials = new Material();
            $materialList = $materials->materialList($map, $page, $pre_page = 20);
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $materialList);

        }
        return $this->asJson($jsonData);
    }

    /**
     * 获取详情
     * @return \yii\web\Response
     */
    public function actionDetail()
    {
        $request = Yii::$app->request;
        $material_id = $request->post('material_id');

        $msg = '';
        $flag = false;

        if (!$material_id) {
            $msg = 'material_id 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(false, $msg);
        } else {
            $materials = new  Material();
            $map = " m.id = {$material_id} AND m.status = 1 ";
            $material = $materials->materialDetail($map);

            if ($material) {
                $jsonData = ResponseHelper::buildJsonData(200, '成功', $material);
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '失败');
            }
        }
        return $this->asJson($jsonData);
    }

    /**
     * 下载文件
     */
    public function actionDownload()
    {
        $request = Yii::$app->request;
        $url = $request->post('url');

        $msg = '';
        $flag = false;

        if (!$url) {
            $msg = 'url 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, '失败', $msg);
            return $this->asJson($jsonData);
        } else {

            //本站下载
            Yii::$app->response->sendFile($url)->send();
        }
    }

    public function actionCount()
    {
        $request = Yii::$app->request;
        $material_id = $request->post('material_id');

        $msg = '';
        $flag = false;

        if (!$material_id) {
            $msg = 'material_id 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, '失败', $msg);
        } else {

            $res = MallMaterial::updateAllCounters(['down_count' => 1], ['id' => $material_id]);
            if ($res) {
                $jsonData = ResponseHelper::buildJsonData(200, '成功');
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '失败');
            }
        }

        return $this->asJson($jsonData);
    }
}